package com.qumitech.visualnovel.service.activity.impl;

import com.alibaba.fastjson.JSON;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.activity.ActivityContentConfigDTO;
import com.qumitech.visualnovel.service.activity.ActivityContentConfigService;
import com.qumitech.visualnovel.service.exception.ExceptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @className: ActivityContentConfigServiceImpl
 * @description:
 * @author: bzhao
 * @date: 2020/10/10 8:54 下午
 * copyright(c) 2017-2020 趣米（武汉）科技有限公司
 */
@Service
public class ActivityContentConfigServiceImpl implements ActivityContentConfigService {

    private static final String ACTIVITY_CONTENT_CONFIG_URL = "/admin/activity_content/config";

    @Autowired
    private ExceptionService exceptionService;

    @Override
    public BaseRsp listActivityContentConf(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + ACTIVITY_CONTENT_CONFIG_URL;
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        return JSON.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp saveActivityContentConf(ActivityContentConfigDTO dto) {
        String url = UrlConstant.SERVER_URL + ACTIVITY_CONTENT_CONFIG_URL;
        String body = HttpInvokeUtil.httpPutJson(url, JSON.toJSONString(dto),"utf-8",null);
        BaseRsp baseRsp = JSON.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(baseRsp);
        return baseRsp;
    }

    @Override
    public BaseRsp updateActivityContentConf(ActivityContentConfigDTO dto) {
        String url = UrlConstant.SERVER_URL + ACTIVITY_CONTENT_CONFIG_URL;
        String body = HttpInvokeUtil.httpPostJson(url, JSON.toJSONString(dto),"utf-8",null);
        BaseRsp baseRsp = JSON.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(baseRsp);
        return baseRsp;
    }

    @Override
    public BaseRsp deleteActivityContentConf(ActivityContentConfigDTO dto) {
        String url = UrlConstant.SERVER_URL + ACTIVITY_CONTENT_CONFIG_URL + "/" + dto.getActId();;
        String body = HttpInvokeUtil.httpDelete(url, new HashMap<>());
        BaseRsp baseRsp = JSON.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(baseRsp);
        return baseRsp;
    }
}
